<template>
	<view class="balance-page">
		<!-- 主要内容 -->
		<scroll-view scroll-y class="container" :style="{height: scrollHeight + 'px'}">
			<!-- 余额卡片 -->
			<view class="balance-card">
				<view class="balance-title">账户余额(元)</view>
				<view class="balance-amount">{{balance.toFixed(2)}}</view>
				<view class="balance-actions">
					<u-button type="primary" shape="circle" text="充值" customStyle="width: 200rpx;"
						@click="showRecharge = true"></u-button>
				</view>
			</view>

			<!-- 交易记录 -->
			<view class="section">
				<view class="section-header">
					<text class="title">交易记录</text>
				</view>

				<view class="record-list">
					<view v-for="(item, index) in records" :key="index" class="record-item">
						<view class="record-left">
							<u-icon :name="item.type === 'recharge' ? 'plus-circle' : 'minus-circle'"
								:color="item.type === 'recharge' ? '#19be6b' : '#ff2d51'" size="20"></u-icon>
							<view class="record-info">
								<view class="record-title">{{item.title}}</view>
								<view class="record-time">{{item.time}}</view>
							</view>
						</view>
						<view class="record-amount" :style="{color: item.type === 'recharge' ? '#19be6b' : '#ff2d51'}">
							{{item.type === 'recharge' ? '+' : '-'}}{{item.amount.toFixed(2)}}
						</view>
					</view>

					<u-empty v-if="records.length === 0" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"
						text="暂无交易记录"></u-empty>
				</view>
			</view>
		</scroll-view>

		<!-- 充值弹窗 -->
		<u-popup :show="showRecharge" mode="bottom" round="16" @close="showRecharge=false">
			<view class="popup-content">
				<view class="popup-title">账户充值</view>
				<view class="amount-input">
					<text class="symbol">¥</text>
					<u-input v-model="rechargeAmount" placeholder="请输入充值金额" type="number" border="none"
						:customStyle="{fontSize: '48rpx', height: '80rpx'}"></u-input>
				</view>

				<view class="quick-amount">
					<text class="quick-title">快捷充值</text>
					<view class="amount-btns">
						<u-button v-for="amount in quickAmounts" :key="amount" size="mini" :text="amount + '元'"
							:plain="rechargeAmount !== amount.toString()"
							:type="rechargeAmount === amount.toString() ? 'primary' : 'default'"
							customStyle="margin-right: 20rpx; margin-bottom: 20rpx"
							@click="rechargeAmount = amount.toString()"></u-button>
					</view>
				</view>

				<u-button type="primary" shape="circle" text="立即充值" customStyle="margin-top: 40rpx"
					@click="handleRecharge"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		httpsend
	} from '@/api/api';
	export default {
		data() {
			return {
				loading: false,
				scrollHeight: 0,
				balance: 0,
				showRecharge: false,
				rechargeAmount: '',
				withdrawAmount: '',
				quickAmounts: [50, 100, 200, 500, 1000],
				records: []
			}
		},
		onLoad() {
			this.calcScrollHeight();
			this.loaddata();
		},
		methods: {
			calcScrollHeight() {
				const systemInfo = uni.getSystemInfoSync();
				this.scrollHeight = systemInfo.windowHeight;
			},

			// 加载数据
			async loaddata() {
				try {
					uni.showLoading({
						title: '加载中...'
					});
					let param = {
						usrid: uni.getStorageSync('usrid'),
					}
					this.loading = true;
					const res = await httpsend('getconsumlist');
					if (res?.code === 1) {
						//消费信息
						this.records = res.list;
						this.balance = res.balance;
					} else {
						this.$api.msg(res.msg);
					}
					this.loading = false;
					uni.hideLoading();
				} catch (err) {
					this.loading = false;
					uni.hideLoading();
					console.log('err', err);
				}
			},
			// 处理充值
			handleRecharge() {
				if (!this.rechargeAmount || isNaN(this.rechargeAmount) || parseFloat(this.rechargeAmount) <= 0) {
					uni.showToast({
						title: '请输入正确的金额',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					title: '处理中...'
				});

				// 模拟API请求
				setTimeout(() => {
					uni.hideLoading();
					this.balance += parseFloat(this.rechargeAmount);

					// 添加充值记录
					this.records.unshift({
						id: Date.now(),
						type: 'recharge',
						title: '账户充值',
						amount: parseFloat(this.rechargeAmount),
						time: this.formatTime(new Date())
					});

					this.showRecharge = false;
					this.rechargeAmount = '';
					uni.showToast({
						title: '充值成功'
					});
				}, 1000);
			},
			// 格式化时间
			formatTime(date) {
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const hours = date.getHours().toString().padStart(2, '0');
				const minutes = date.getMinutes().toString().padStart(2, '0');

				return `${year}-${month}-${day} ${hours}:${minutes}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.balance-page {
		height: 100vh;
		background-color: #f7f7f7;
	}

	.container {
		padding: 20rpx;
	}

	.balance-card {
		background: linear-gradient(135deg, #ff2d51, #ff6b81);
		border-radius: 16rpx;
		padding: 40rpx 30rpx;
		color: #fff;
		margin-bottom: 20rpx;
		display: flex;
		align-items: flex-start;
		flex-wrap: wrap;
		flex-direction: column;

		.balance-title {
			font-size: 28rpx;
			margin-bottom: 20rpx;
		}

		.balance-amount {
			font-size: 60rpx;
			font-weight: bold;
			margin-bottom: 30rpx;
		}

		.balance-actions {}
	}

	.section {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 0 30rpx;

		.section-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 80rpx;
			border-bottom: 1rpx solid #f5f5f5;

			.title {
				font-size: 28rpx;
				font-weight: bold;
			}

			.more {
				font-size: 24rpx;
				color: #999;
			}
		}
	}

	.record-list {
		padding: 20rpx 0;

		.record-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 25rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

			&:last-child {
				border-bottom: none;
			}

			.record-left {
				display: flex;
				align-items: center;
			}

			.record-info {
				margin-left: 20rpx;

				.record-title {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 6rpx;
				}

				.record-time {
					font-size: 24rpx;
					color: #999;
				}
			}

			.record-amount {
				font-size: 30rpx;
				font-weight: bold;
			}
		}
	}

	.popup-content {
		padding: 40rpx;

		.popup-title {
			font-size: 32rpx;
			font-weight: bold;
			text-align: center;
			margin-bottom: 40rpx;
		}

		.amount-input {
			display: flex;
			align-items: center;
			border-bottom: 1rpx solid #f5f5f5;
			padding-bottom: 20rpx;
			margin-bottom: 40rpx;

			.symbol {
				font-size: 48rpx;
				margin-right: 20rpx;
			}
		}

		.quick-amount {
			margin-bottom: 40rpx;

			.quick-title {
				display: block;
				font-size: 26rpx;
				color: #999;
				margin-bottom: 20rpx;
			}

			.amount-btns {
				display: flex;
				flex-wrap: wrap;
			}
		}

		.withdraw-info {
			display: flex;
			justify-content: space-between;
			font-size: 28rpx;
			margin-bottom: 30rpx;

			.withdraw-amount {
				color: #ff2d51;
				font-weight: bold;
			}
		}

		.bank-card {
			display: flex;
			align-items: center;
			padding: 30rpx;
			background-color: #f7f7f7;
			border-radius: 12rpx;
			margin-top: 30rpx;

			.card-info {
				flex: 1;
				margin-left: 20rpx;

				.card-name {
					font-size: 28rpx;
					margin-bottom: 10rpx;
				}

				.card-type {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}
</style>